﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class AreaController : BaseManager<AreaController>
{
    [SerializeField]
    public List<float[]> dotList = new List<float[]>();  //点列表
    public List<float[]> gdotList = new List<float[]>(); //重心列表
    public List<float> areaList = new List<float>(); //划分的面积列表
    int[] GPosition = new int[2];
    float eCxs = 0;
    float eCys = 0;
    float eS = 0;
    public Vector3 CreatCenterDot(List<Vector3> baseList)
    {
        for (int i = 0; i < baseList.Count; i++)
        {
            float[] pos = new float[2];
            pos[0] = baseList[i].z;
            pos[1] = baseList[i].x;
            dotList.Add(pos);
        }
        if (dotList.Count >= 3)
        {
            for (int i = 1; i <= dotList.Count - 2; i++)
            {
                //求重心坐标
                float[] c = new float[2] { (dotList[0][1] + dotList[i][1] + dotList[i + 1][1]), (dotList[0][0] + dotList[i][0] + dotList[i + 1][0]) };
                gdotList.Add(c);
               // Debug.Log($"第{i}个三角形重心坐标:{c[0]} {c[1]}");
                //求面积
                float s = ((dotList[i][1] - dotList[0][1]) * (dotList[i + 1][0] - dotList[0][0]) - (dotList[i + 1][1] - dotList[0][1]) * (dotList[i][0] - dotList[0][0])) / 2;
                areaList.Add(s);
               // Debug.Log($"第{i}个三角形面积坐标:{s}");
            }
            //求重心x与面积的积的和
            for (int i = 0; i < dotList.Count - 2; i++)
            {
                eCxs = eCxs + gdotList[i][0] * areaList[i];
            }
            for (int i = 0; i < dotList.Count - 2; i++)
            {
                eCys = eCys + gdotList[i][1] * areaList[i];
            }
            for (int i = 0; i < dotList.Count - 2; i++)
            {
                eS = eS + areaList[i];
            }
            GPosition[0] = Mathf.RoundToInt(eCys / (3 * eS));
            GPosition[1] = Mathf.RoundToInt(eCxs / (3 * eS));
           // Debug.Log(GPosition[0] + "*" + GPosition[1]);
            // MapContorller.GetInstance().ArrayGrid[GPosition[0], GPosition[1]].ChangeMaterial(GameManager.GetInstance().material[2]);
            /******************************生成阵眼，阵边缘******************************************/
            // GameObject dot = GameObject.Instantiate(Resources.Load<GameObject>("ObjectRes/Dot"));
            // dot.transform.position = new Vector3(GPosition[1], 0.3f, GPosition[0]);
            // dot.transform.LookAt(Camera.main.gameObject.transform.position);
        }
        else
        {
            Debug.LogError($"选择的阵旗位置少于3或间隔距离少于3");
        }
        eCxs = 0;
        eCys = 0;
        eS = 0;
        dotList.Clear();
        gdotList.Clear();
        areaList.Clear();

        return new Vector3(GPosition[1], 0.3f, GPosition[0]);
    }

    public void CreatCenterDot2(List<BaseGrid> baseList)
    {
        for (int i = 0; i < baseList.Count; i++)
        {
            dotList.Add(baseList[i].objPosition);
        }
        if (dotList.Count >= 3)
        {
            for (int i = 1; i <= dotList.Count - 2; i++)
            {
                //求重心坐标
                float[] c = new float[2] { (dotList[0][1] + dotList[i][1] + dotList[i + 1][1]), (dotList[0][0] + dotList[i][0] + dotList[i + 1][0]) };
                gdotList.Add(c);
                Debug.Log($"第{i}个三角形重心坐标:{c[0]} {c[1]}");
                //求面积
                float s = ((dotList[i][1] - dotList[0][1]) * (dotList[i + 1][0] - dotList[0][0]) - (dotList[i + 1][1] - dotList[0][1]) * (dotList[i][0] - dotList[0][0])) / 2;
                areaList.Add(s);
                Debug.Log($"第{i}个三角形面积坐标:{s}");
            }
            //求重心x与面积的积的和
            for (int i = 0; i < dotList.Count - 2; i++)
            {
                eCxs = eCxs + gdotList[i][0] * areaList[i];
            }
            for (int i = 0; i < dotList.Count - 2; i++)
            {
                eCys = eCys + gdotList[i][1] * areaList[i];
            }
            for (int i = 0; i < dotList.Count - 2; i++)
            {
                eS = eS + areaList[i];
            }
            GPosition[0] = Mathf.RoundToInt(eCys / (3 * eS));
            GPosition[1] = Mathf.RoundToInt(eCxs / (3 * eS));
            Debug.Log(GPosition[0] + "*" + GPosition[1]);
            //MapContorller.GetInstance().ArrayGrid[GPosition[0], GPosition[1]].ChangeMaterial(GameManager.GetInstance().material[2]);
            /******************************生成阵眼，阵边缘******************************************/
            GameObject dot = GameObject.Instantiate(Resources.Load<GameObject>("ObjectRes/Dot"));
            dot.transform.position = new Vector3(GPosition[1], 0.3f, GPosition[0]);
            dot.transform.LookAt(Camera.main.gameObject.transform.position);
        }
        else
        {
            Debug.LogError($"选择的阵旗位置少于3或间隔距离少于3");
        }
        eCxs = 0;
        eCys = 0;
        eS = 0;
        dotList.Clear();
        gdotList.Clear();
        areaList.Clear();
    }
}
